<?php namespace Manage;

class Group extends Base {

	protected $table = 'agents_group';

    protected $fillable=array('location');
	/**
	 * 获取代理商分组信息(标签)
	 * @return [type]     [description]
	 */
	static public function getGroup()
	{

		return Group::where('agent_id', parent::getAgentId())->lists('name', 'id');

	}

	/**
	 * 获取代理商分组信息
	 * @return [type]     [description]
	 */
	static public function getGroupDefault()
	{
		$page = $_ENV['PAGINATE'];

		//获取小区基本信息
		$groupInfo = Group::where('agent_id', parent::getAgentId())->paginate($page);

		//获取小区设备数
		$devData = \DB::table('agents_device')->select(\DB::raw('count(device_id) as dev_count'), 'group_id')->where('agent_id', parent::getAgentId())->groupBy('group_id')->get();

		//小区对应设备数整合
		$groupData = array();
		foreach ($devData as $v) 
		{
			$groupData[$v->group_id] = $v->dev_count;
		}

		foreach ( $groupInfo as $k => $val ) 
		{
			$groupInfo[$k]->num = isset($groupData[$val->id])?$groupData[$val->id]:0;
		}

		return $groupInfo;

	}

	/**
	 * 获取指定分组信息
	 * @param int $gid 分组ID
	 * @return [type]     [description]
	 */
	static public function getGroupById($gid)
	{
		return Group::where('id', $gid)->get();
	}

	/**
	 * 修改指定分组信息
	 * @param int $gid 分组ID
	 * @param array $data 修改数据
	 * @return [type]     [description]
	 */
	static public function editGroupById($gid, $data)
	{
		return Group::where('id', $gid)->update($data);
	}

	/**
	 * 新建分区
	 * @param  [type] $name 分区名
	 * @return [type]       [description]
	 */
	static public function createGroup($uid, $name, $location = 0)
	{
		$data['agent_id'] = $uid;
		$data['name'] = $name;
		$data['location'] = $location;

		return Group::insertGetId($data);

	}

	/**
	 * 新建分区(默认)
	 * @param  array $data 分区名
	 * @return [type]       [description]
	 */
	static public function createGroupDefault($data)
	{
		$data['agent_id'] = parent::getAgentId();
		$data['created_at'] = date('Y-m-d H:i:s');
		return Group::insertGetId($data);

	}

	/**
	 * 修改设备所属分区
	 * @param [type] $uid 代理商ID
	 * @param [type] $did 设备ID
	 * @param [type] $gid 分组ID
	 */
	static public function bindGroup($uid, $did, $gid)
	{
		$where['agent_id'] = $uid;
		$where['device_id'] = $did;

        $result = \DB::table('agents_device')->where($where)->first();

		if($result)
		{
            \DB::table('agents_device')->where($where)->update(array('group_id' => $gid));
			return $gid;
		}else{
            $where['group_id']=$gid;
            \DB::table('agents_device')->insert($where);
            return $gid;
        }
	}


	/**
	 * 批量修改设备所属分区
	 * @param [type] $dIds 设备ID组
	 * @param [type] $gId 分组ID
	 */
	static public function changeGroup($gId, $dIds)
	{

		//获取代理商设备-分组
		$agentId = parent::getAgentId();
		$devices = \DB::table('agents_device')->where('agent_id', $agentId)->whereIn('device_id', $dIds)->lists('group_id','device_id');

		//操作更新
		\DB::table('agents_device')->where('group_id', $gId)->update(array('group_id' => 0));
		$result = \DB::table('agents_device')->where('agent_id', $agentId)->whereIn('device_id', $dIds)->update(array('group_id' => $gId));
		if( $result )
		{
			return true;
		}
		return false;
	}

	/**
	 * 获取小区设备列表
	 * @param [type] $id 分组ID
	 */
	static public function getDevListByGroupId($id)
	{
		//获取设备ID集
		$devIds = \DB::table('agents_device')->where('group_id', $id)->lists('device_id');

		//获取设备信息
		$devInfos = \DB::table('devices')->select('id', 'title', 'address')->whereIn('id', $devIds)->get();

		return $devInfos;
	}
	

}
